using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Input;
using Avalonia.Media;
using Digitalmes.AvaloniaUI.Behaviors.DragAndDrop;

namespace Digitalmes.AvaloniaUI.Pages;

public partial class DragDemoPage : UserControl
{
    private Control? _selectedShape; // 当前选中的图形

    public DragDemoPage()
    {
        InitializeComponent();

        var dragDrawCanvas = new DragDrawCanvas(MainCanvas);
        dragDrawCanvas.EnableInteractions();
        dragDrawCanvas.OnShapeSelected = (shape) =>
        {
            if (shape is null)
            {
                HidePropertyPanel();
            }
            else
            {
                SelectShape(shape);
            }
        };

        // 订阅键盘事件
        AddHandler(KeyDownEvent, OnKeyDown, handledEventsToo: true);
    }

    // 开始拖动图形
    private async void OnShapeDragStart(object? sender, PointerPressedEventArgs e)
    {
        await DragDrawCanvas.OnDragDrop(sender, e);
    }

    // 选中图形
    private void SelectShape(Control shape)
    {
        if (_selectedShape != null && _selectedShape is Control shape2)
        {
            // 取消之前选中的高亮
            //shape2.Stroke = null;
            //shape2.StrokeThickness = 0;
        }

        // 设置新选中的图形
        _selectedShape = shape;
        UpdatePropertyPanel(shape);
        if (_selectedShape is Control shape1)
        {
            //shape1.Stroke = Brushes.Orange;
            //shape1.StrokeThickness = 4; // 添加高亮
        }
    }

    // 键盘事件处理
    private void OnKeyDown(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Delete && _selectedShape != null)
        {
            // 删除选中图形
            MainCanvas.Children.Remove(_selectedShape);
            _selectedShape = null; // 清空选中状态
        }
    }

    // 更新属性面板
    private void UpdatePropertyPanel(Control shape)
    {
        // 显示面板
        PropertyPanel.IsVisible = true;

        // 获取属性并填充面板数据
        PositionXInput.Text = Canvas.GetLeft(shape).ToString();
        PositionYInput.Text = Canvas.GetTop(shape).ToString();
        WidthInput.Text = shape.Width.ToString();
        HeightInput.Text = shape.Height.ToString();
    }

    // 隐藏属性面板
    private void HidePropertyPanel()
    {
        PropertyPanel.IsVisible = false;
        _selectedShape = null;
    }

    // 当属性被修改时更新图形
    private void OnPositionXChanged(object? sender, TextChangedEventArgs e)
    {
        if (_selectedShape != null && double.TryParse(PositionXInput.Text, out double x))
        {
            Canvas.SetLeft(_selectedShape, x);
        }
    }

    private void OnPositionYChanged(object? sender, TextChangedEventArgs e)
    {
        if (_selectedShape != null && double.TryParse(PositionYInput.Text, out double y))
        {
            Canvas.SetTop(_selectedShape, y);
        }
    }

    private void OnWidthChanged(object? sender, TextChangedEventArgs e)
    {
        if (_selectedShape != null && double.TryParse(WidthInput.Text, out double width))
        {
            _selectedShape.Width = width;
        }
    }

    private void OnHeightChanged(object? sender, TextChangedEventArgs e)
    {
        if (_selectedShape != null && double.TryParse(HeightInput.Text, out double height))
        {
            _selectedShape.Height = height;
        }
    }

    // 取消选中
    private void OnCanvasClicked(object? sender, PointerPressedEventArgs e)
    {
        HidePropertyPanel();
    }
}
